﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using Interpol.GameScreens;
using SpriteObjects;
using GameEntity;

namespace Interpol
{
    public class MenuScreen : Interpol.GameScreens.GameScreen
    {
        My2DSprite mySprite;
        List<Item> mainMenu;
        int iMenuItem;
        bool isKeyUpDown = false;
        bool isKeyDownDown = false;
        Texture2D background;
        SpriteFont menuFont;
        bool flag = false; //Neu co Item duoc chon thi flag = true;
        bool canDrop = true;
        bool input = true; //True = use keyboard, false = use mouse
        bool isChosen = false; //true = mouse press vao mot Item

        public MenuScreen(ContentManager content)
        {
            menuFont = content.Load<SpriteFont>("menufont");
            Texture2D[] textures;
            textures = new Texture2D[10];
            for (int i = 0; i < 10; i++)
            {
                textures[i] = content.Load<Texture2D>("Sprites/GirlWalking/IMG" + i.ToString("00000"));
            }
            mySprite = new My2DSprite(textures, new Vector2(400, 100));
            mySprite.NormalDelay = 50.0f;
            background = content.Load<Texture2D>("Backgrounds/Background01");
            
            //Tao Item tren main menu
            mainMenu = new List<Item>();
            iMenuItem = -1;

            LoadMenuFromXMLFile(content);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(background, spriteBatch.GraphicsDevice.Viewport.Bounds, Color.White);
            if (flag)
            {
                spriteBatch.DrawString(menuFont, "Da chon: ", new Vector2(400, 50), Color.Orange);
                spriteBatch.DrawString(menuFont, mainMenu[iMenuItem].Name, new Vector2(450, 100), Color.Red);
            }
            foreach (Item item in mainMenu)
            {
                item.Draw(gameTime, spriteBatch);
            }
            mySprite.Draw(gameTime, spriteBatch);
            spriteBatch.End();
        }


        public void Update(GameTime gameTime)
        {
            CheckKeyboard(gameTime);

            CheckMouse(gameTime);

            mySprite.Update(gameTime);
        }

        //====================================================================//

        private void LoadMenuFromXMLFile(ContentManager content)
        {
            XmlDocument xdoc = new XmlDocument();
            XmlNode rootNode = null;
            xdoc.Load("...\\...\\...\\MenuDescripton.xml");
            rootNode = xdoc.SelectSingleNode("MENU");

            XmlNodeList nodeItem = rootNode.SelectNodes("Item");

            for (int i = 0; i < nodeItem.Count; i++)
            {
                string name = ((XmlElement)nodeItem[i]).GetAttribute("name");
                string assetName = ((XmlElement)nodeItem[i]).GetAttribute("assetName");
                int x = int.Parse(((XmlElement)nodeItem[i]).GetAttribute("x"));
                int y = int.Parse(((XmlElement)nodeItem[i]).GetAttribute("y"));

                Texture2D[] textures;
                textures = new Texture2D[2];
                for (int j = 0; j < 2; j++)
                {
                    textures[j] = content.Load<Texture2D>(assetName + j.ToString("00"));
                }

                Item item = new Item(textures, new Vector2(x, y), name);
                mainMenu.Add(item);
            }
        }

        private void CheckMouse(GameTime gameTime)
        {
            //Kiem tra mouse
            MouseState mouseState = Mouse.GetState();
            if (canDrop && mouseState.LeftButton == ButtonState.Pressed)
            {
                flag = false;
                if (input == true)
                {
                    mainMenu[iMenuItem].Update(gameTime);
                    input = false;
                }
                for (int i = 0; i < mainMenu.Count; i++)
                    if (mainMenu[i].IsClicked(new Vector2((float)mouseState.X, (float)mouseState.Y)))
                    {
                        iMenuItem = i;
                        mainMenu[iMenuItem].Update(gameTime);
                        isChosen = true;
                        break;
                    }
                canDrop = false;
            }
            else if (mouseState.LeftButton == ButtonState.Released && input == false)
            {
                if (canDrop == false && isChosen == true)
                {
                    mainMenu[iMenuItem].Update(gameTime);
                    isChosen = false;
                }
                int temp = -2;
                for (int i = 0; i < mainMenu.Count; i++)
                    if (mainMenu[i].IsClicked(new Vector2((float)mouseState.X, (float)mouseState.Y)))
                    {
                        temp = i;
                        break;
                    }
                if (temp == iMenuItem)
                {
                    flag = true;
                    if (iMenuItem == mainMenu.Count - 1)
                    {
                        System.Environment.Exit(System.Environment.ExitCode);
                    }
                }
                canDrop = true;
            }
        }

        private void CheckKeyboard(GameTime gameTime)
        {
            // Kiem tra keyboard
            if (iMenuItem == -1)
            {
                iMenuItem = 0;
                mainMenu[iMenuItem].Update(gameTime);
            }
            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Up) && isKeyUpDown == false)
            {
                if (input == false)
                {
                    mainMenu[iMenuItem].Update(gameTime);
                    input = true;
                }
                isKeyUpDown = true;
                if (iMenuItem != 0)
                {
                    flag = false;
                    mainMenu[iMenuItem].Update(gameTime);
                    mainMenu[iMenuItem - 1].Update(gameTime);
                    iMenuItem--;
                }
            }
            if (keyboardState.IsKeyUp(Keys.Up) && isKeyUpDown == true)
                isKeyUpDown = false;

            if (keyboardState.IsKeyDown(Keys.Down) && isKeyDownDown == false)
            {
                if (input == false)
                {
                    mainMenu[iMenuItem].Update(gameTime);
                    input = true;
                }
                isKeyDownDown = true;

                if (iMenuItem != mainMenu.Count - 1)
                {
                    flag = false;
                    mainMenu[iMenuItem].Update(gameTime);
                    mainMenu[iMenuItem + 1].Update(gameTime);
                    iMenuItem++;
                }
            }
            if (keyboardState.IsKeyUp(Keys.Down) && isKeyDownDown == true)
                isKeyDownDown = false;

            if (keyboardState.IsKeyDown(Keys.Enter) && iMenuItem != -1)
            {
                flag = true;
                if (iMenuItem == mainMenu.Count - 1)
                {
                    System.Environment.Exit(System.Environment.ExitCode);
                }

            }
        }
    }
}
